n = int(input())
m = 0
if 999<n<10000:
    n1 = n // 1000
    n2 = n // 100 % 10
    n3 = n // 10 % 10
    n4 = n % 1000 % 10
    n12 = n // 100
    n34 = n % 1000 // 10
    n123 = n // 10
    n234 = n % 1000
    a = [n1,n2,n3,n4,n12,n34,n123,n234]
    for i in a:
        for j in range(2, i):
            if i % j == 0:
                break
        else:
            m = max(m, i)
    print(m)
else:
   print(0)